
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import io.goeasy.otp.OtpGenerator;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Properties;

@Slf4j
public class test {
    public static void main(String[] args) throws NacosException {

        /*String nacosServerAddr = "10.186.40.122:31488";
        String namespace = "task-center";
        String serviceName = "task-center-dev";
        String username = "nacos";
        String password = "Ha5!m8ry";

        // 初始化Nacos客户端
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosServerAddr);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);
        properties.put(PropertyKeyConst.USERNAME, username);
        properties.put(PropertyKeyConst.PASSWORD, password);
        NamingService namingService = NacosFactory.createNamingService(properties);

        // 获取服务实例列表
        List<Instance> instances = namingService.getAllInstances(serviceName);
        for (Instance instance : instances) {
            System.out.println("instance = " + JSON.toJSONString(instance));
        }*/

    }
    public static String goEasyOTP(String secretKey) {
        try {
            String otp = "000" + System.currentTimeMillis();
            SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            byte[] otpBytes = otp.getBytes();
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedOTP = cipher.doFinal(otpBytes);
            otp = new BASE64Encoder().encode(encryptedOTP);
            return otp;
        } catch (Exception e) {
            throw new IllegalStateException("Failed to generate GoEasy-OTP.", e);
        }
    }
}
